<?php

namespace app\admin\controller\cases;

use app\admin\model\userstaff\User;
use app\admin\model\userstaff\UserClass;
use app\common\controller\Backend;
use think\Db;
use think\exception\DbException;
use think\exception\PDOException;
use think\exception\ValidateException;
use think\response\Json;

/**
 * 
 *
 * @icon fa fa-circle-o
 */
class Cases extends Backend
{

    /**
     * Cases模型对象
     * @var \app\admin\model\cases\Cases
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\cases\Cases;
        $this->modelUser = new User();
        $this->modelShop = new \app\admin\model\shop\User();
        $this->modelType = new \app\admin\model\cases\Ctype();
        $this->modelUserClass = new UserClass();
        $this->view->assign("typeList", $this->model->getTypeList());
        $this->view->assign("statusList", $this->model->getStatusList());
        $this->view->assign("isVerifyList", $this->model->getIsVerifyList());
        $this->view->assign("isAppearList", $this->model->getIsAppearList());
    }



    /**
     * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
     * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
     * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
     */

    /**
     * 查看
     *
     * @return string|Json
     * @throws \think\Exception
     * @throws DbException
     */
    public function index()
    {
        //设置过滤方法
        $this->request->filter(['strip_tags', 'trim']);
        if (false === $this->request->isAjax()) {
            return $this->view->fetch();
        }
        //如果发送的来源是 Selectpage，则转发到 Selectpage
        if ($this->request->request('keyField')) {
            return $this->selectpage();
        }
        [$where, $sort, $order, $offset, $limit] = $this->buildparams();
        $list = $this->model
            ->where($where)
            ->order($sort, $order)
            ->paginate($limit);
        foreach ($list->items() as &$v){
            $v['detachment_name'] = $this->modelUser->where('id',$v['detachment_id'])->value('name') ?? '';
            $v['captain_name'] = $this->modelUser->where('id',$v['captain_id'])->value('name') ?? '';
            $v['user_staff_name'] = $this->modelUser->where('id',$v['user_staff_id'])->value('name') ?? '';
            $v['accept_name'] = $this->modelUser->where('id',$v['accept_id'])->value('name') ?? '';
            $v['user_shop_name'] = $this->modelShop->where('id',$v['user_shop_id'])->value('name') ?? '';
            $v['cases_type_name'] = $this->modelType->where('id',$v['cases_type_id'])->value('name') ?? '';
            $v['user_staff_class_name'] = $this->modelUserClass->where('id',$v['user_staff_class_id'])->value('name') ?? '';
        }
        $result = ['total' => $list->total(), 'rows' => $list->items()];
        return json($result);
    }


    /**
     * 编辑
     *
     * @param $ids
     * @return string
     * @throws DbException
     * @throws \think\Exception
     */
    public function detail($ids = null)
    {
        $row = $this->model->get($ids);
        if (!$row) {
            $this->error(__('No Results were found'));
        }
        $adminIds = $this->getDataLimitAdminIds();
        if (is_array($adminIds) && !in_array($row[$this->dataLimitField], $adminIds)) {
            $this->error(__('You have no permission'));
        }
        if (false === $this->request->isPost()) {
            $row['detachment_name'] = $this->modelUser->where('id',$row['detachment_id'])->value('name') ?? '';
            $row['captain_name'] = $this->modelUser->where('id',$row['captain_id'])->value('name') ?? '';
            $row['user_staff_name'] = $this->modelUser->where('id',$row['user_staff_id'])->value('name') ?? '';
            $row['accept_name'] = $this->modelUser->where('id',$row['accept_id'])->value('name') ?? '';
            $row['user_shop_name'] = $this->modelShop->where('id',$row['user_shop_id'])->value('name') ?? '';
            $row['cases_type_name'] = $this->modelType->where('id',$row['cases_type_id'])->value('name') ?? '';
            $row['user_staff_class_name'] = $this->modelUserClass->where('id',$row['user_staff_class_id'])->value('name') ?? '';
            $this->view->assign('row', $row);
            return $this->view->fetch();
        }
        $this->success();
    }

}
